package com.dican.portal.pedidos;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.dican.portal.DAO.ClientesDAO;
import com.dican.portal.beans.Representante;

public class FillCli extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public FillCli() {
        super();
        
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession(false);
		if(session==null)
			response.sendRedirect("logout.jsp");
		Representante rep = (Representante)session.getAttribute("rep");
		try{
			if(request.getParameter("cliId")==null)
				request.setAttribute("clientes", ClientesDAO.retrieveAllLinhas(rep.getBpid()));
			else
				request.setAttribute("clientes", ClientesDAO.getCliente(request.getParameter("clientes")));
			RequestDispatcher view = request.getRequestDispatcher("digitarpedido.jsp");
			view.forward(request, response);
		}catch(SQLException e){
			out.print("SQL error "+e.getMessage());
		}catch(Exception e){
			out.print("Erro no FillCli.<br>Contate o Suporte da Dican e informe o seguinte erro:<br>"+e.getMessage());
		}
	}
}
